package org.jeecg.modules.bv.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.bv.entity.BvSubVouchers;
import org.jeecg.modules.bv.entity.BvVouchers;
import org.jeecg.modules.bv.mapper.BvSubVouchersMapper;
import org.jeecg.modules.bv.mapper.BvVouchersMapper;
import org.jeecg.modules.bv.service.IBvVouchersService;
import org.jeecg.modules.bv.vo.BvIndexDataVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @Description: 票据使用
 * @Author: jeecg-boot
 * @Date:   2022-03-09
 * @Version: V1.0
 */
@Service
public class BvVouchersServiceImpl extends ServiceImpl<BvVouchersMapper, BvVouchers> implements IBvVouchersService {

	@Autowired
	private BvVouchersMapper bvVouchersMapper;
	@Autowired
	private BvSubVouchersMapper bvSubVouchersMapper;

	@Override
	@Transactional
	public void saveMain(BvVouchers bvVouchers, List<BvSubVouchers> bvSubVouchersList) {
		bvVouchersMapper.insert(bvVouchers);
		if(bvSubVouchersList!=null && bvSubVouchersList.size()>0) {
			for(BvSubVouchers entity:bvSubVouchersList) {
				//外键设置
				entity.setVoucherId(bvVouchers.getId());
				bvSubVouchersMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(BvVouchers bvVouchers,List<BvSubVouchers> bvSubVouchersList) {
		bvVouchersMapper.updateById(bvVouchers);

		//1.先删除子表数据
		bvSubVouchersMapper.deleteByMainId(bvVouchers.getId());

		//2.子表数据重新插入
		if(bvSubVouchersList!=null && bvSubVouchersList.size()>0) {
			for(BvSubVouchers entity:bvSubVouchersList) {
				//外键设置
				entity.setVoucherId(bvVouchers.getId());
				bvSubVouchersMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		bvSubVouchersMapper.deleteByMainId(id);
		bvVouchersMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			bvSubVouchersMapper.deleteByMainId(id.toString());
			bvVouchersMapper.deleteById(id);
		}
	}

	@Override
	public BvVouchers getNextVouchers(String billType, String versionId) {

		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

		List<BvVouchers>  vouchers= bvVouchersMapper.selectList(new LambdaQueryWrapper<BvVouchers>()
				.eq(BvVouchers::getBillTypeId, billType) //票据类型
				.eq(BvVouchers::getVersionId, versionId)//票据版本号ID
				.eq(BvVouchers::getBillStatusId, "01") //入库状态
				.eq(BvVouchers::getGetOperatId,sysUser.getUsername())//领用人
				.eq(BvVouchers::getStatus, "1")  //正常
				.eq(BvVouchers::getDelFlag, 0) //是否删
				.orderByAsc(BvVouchers::getVoucherNo)); //按票据号排序 升序
		if(null != vouchers && vouchers.size()>0){
			return vouchers.get(0);
		}

		return null;
	}

	@Override
	public List<BvVouchers> openedBillList(String accCode, String oldVersion
			, String oldVoucher, String createTimeBegin, String createTimeEnd) {
		return bvVouchersMapper.openedBillList(accCode,oldVersion,oldVoucher,createTimeBegin,createTimeEnd);
	}

	@Override
	public List<Map<String, Object>> getJimuReport(String dbCode,String tenant) {
		return bvVouchersMapper.getJimuReport(dbCode,tenant);
	}

	@Override
	public List<BvIndexDataVO> queryBvIndexData() {
		return bvVouchersMapper.queryBvIndexData();
	}

}
